home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / gofer221.zip / CH06 < prev    next >
Text File  |  1991-11-20  |  10KB  |  265 lines

  1.  
  2.  
  3. Introduction to Gofer         6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS     
  4.  
  5.  
  6. 6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS
  7.  
  8. As the examples of the previous section show, there are  two  kinds  of
  9. name that can be used for a function; identifiers  such  as  "sum"  and
  10. operator symbols such as "+" and "*".  Choosing the appropriate kind of
  11. name for a particular function  can  often  help  to  make  expressions
  12. involving that function easier to read.  If for  example  the  addition
  13. function was represented by the name "plus" rather  than  the  operator
  14. symbol "+" then the sum of the integers from 1 to 5 would  have  to  be
  15. written as:
  16.  
  17.                  plus (plus (plus (plus 1 2) 3) 4) 5
  18.  
  19. In this particular case, another way of writing the same sum is:
  20.  
  21.                  plus 1 (plus 2 (plus 3 (plus 4 5)))
  22.  
  23. Not only does the use of the identifier "plus" make  these  expressions
  24. larger and more difficult to read than the equivalent expressions using
  25. "+"; it  also  makes  it  very  much  harder  to  see  that  these  two
  26. expressions do actually have the same value.
  27.  
  28. Gofer distinguishes between the two types of name according to the  way
  29. that they are written:
  30.  
  31.   o  An  identifier  begins with a  letter  of the  alphabet optionally
  32.      followed by a sequence of characters, each of which  is  either  a
  33.      letter,  a  digit,  an  apostrophe  (')  or   an   underbar   (_).
  34.      Identifiers representing functions or variable must begin  with  a
  35.      lower case letter (identifiers beginning with an upper case letter
  36.      are  used  to  denote  a  special  kind  of  function   called   a
  37.      `constructor function' described in section 11.1).  The  following
  38.      identifiers are examples of Gofer variable and function names:
  39.  
  40.        sum    f    f''    integerSum    african_queen    do'until'zero
  41.  
  42.      The following identifiers are reserved words in Gofer  and  cannot
  43.      be used as the name of a function or variable:
  44.  
  45.          case      of         where      let        in         if
  46.          then      else       data       type       infix      infixl
  47.          infixr    primitive  class      instance
  48.  
  49.   o  An  operator symbol  is written using one or more of the following
  50.      symbol characters:
  51.  
  52.          :  !  #  $  %  &  *  +  .  /  <  =  >  ?  @  \  ^  |  -
  53.  
  54.      In addition, the tilde character (~) is also  permitted,  although
  55.      only in the first position of an  operator  name.   [N.B.  Haskell
  56.      also makes the  same  restriction  for  the  minus/dash  character
  57.      (-)].   Operator  names  beginning  with  a  colon  are  used  for
  58.      constructor functions in the same  way  as  identifiers  beginning
  59.      with a capital  letter  as  mentioned  above.   In  addition,  the
  60.      following operator symbols have special uses in Gofer:
  61.  
  62.  
  63.  
  64.                                       8
  65.  
  66.  
  67.  
  68.  
  69. Introduction to Gofer         6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS     
  70.  
  71.  
  72.          ::    =    ..    @    \    |    <-    ->    ~    =>
  73.  
  74.      All other operators symbols can be used as variables  or  function
  75.      names, including each of the following examples:
  76.  
  77.          +    ++    &&    ||     <=    ==    /=    //  .
  78.          ==>  $     @@     -*-   \/    /\    ...   ?
  79.  
  80.      [Note that each of the symbols in the first line is  used  in  the
  81.      standard prelude.  If you are interested in using Gofer to develop
  82.      programs for use with a Haskell compiler, you might also  want  to
  83.      avoid using the operator symbols := ! :+ and :% which are used  to
  84.      support features in Haskell not currently provided  by  the  Gofer
  85.      standard prelude.]
  86.  
  87. Gofer provides two simple mechanisms which make it possible to  use  an
  88. identifier  as  an  operator  symbol,  or  an  operator  symbol  as  an
  89. identifier:
  90.  
  91.   o  Any  identifier  will be treated as an  operator  symbol  if it is
  92.      enclosed in backquotes (`) -- for example, the  expressions  using
  93.      the "plus" function above are a little easier to read  using  this
  94.      technique:
  95.  
  96.                (((1 `plus` 2) `plus` 3) `plus` 4) `plus` 5
  97.  
  98.      In general, an expression of the form "x `op` y" is equivalent  to
  99.      the corresponding expression "op x y", whilst an  expression  such
  100.      as "f x y z" can also be written as "(x `f` y) z".
  101.  
  102.      [NOTE: For those using Gofer on a  PC,  you  may  find  that  your
  103.      keyboard does not have a backquote key!  In this case  you  should
  104.      still be able to enter a backquote by holding down the key  marked
  105.      ALT, pressing the keys '9' and then '6' on the numeric keypad  and
  106.      then releasing the ALT key.]
  107.  
  108.   o  Any  operator symbol  can be treated as an identifier by enclosing
  109.      it in parentheses.  For example, the addition function denoted  by
  110.      the operator symbol "+" is often written as "(+)".  Any expression
  111.      of the form "x + y" can also be written in the form "(+) x y".
  112.  
  113. There are two more technical problems which have to be dealt with  when
  114. working with operator symbols:
  115.  
  116.   o  Precedence: Given operator symbols (+) and (*), should "2 * 3 + 4"
  117.      be treated as either "(2 * 3) + 4" or "2 * (3 + 4)"?
  118.  
  119.      This problem is solved by assigning  each  operator  a  precedence
  120.      value (an integer in the range 0 to 9).  In a  situation  such  as
  121.      the  above,  we  simply  compare  the  precedence  values  of  the
  122.      operators involved, and carrying out  the  calculation  associated
  123.      with  the  highest  precedence  operator  first.    The   standard
  124.      precedence values for (+) and (*) are 6 and 7 respectively so that
  125.      the expression above will actually be treated as "(2 * 3) + 4".
  126.  
  127.   o  Grouping: The above rule  is only useful when the operator symbols
  128.  
  129.  
  130.                                       9
  131.  
  132.  
  133.  
  134.  
  135. Introduction to Gofer         6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS     
  136.  
  137.  
  138.      involved have  distinct  precedences.   For  example,  should  the
  139.      expression "1 - 2 - 3" be treated as either "(1 - 2) - 3" giving a
  140.      result of -4, or as "1 - (2 - 3)" giving a result of 2?
  141.  
  142.      This problem is  solved  by  giving  each  operator  a  `grouping'
  143.      (sometimes called its associativity).  An operator symbol  (-)  is
  144.      said to:
  145.  
  146.        o  group to the left  if "x - y - z" is treated as "(x - y) - z"
  147.  
  148.        o  group to the right if "x - y - z" is treated as "x - (y - z)"
  149.  
  150.      A third possibility is that an expression of the form "x - y -  z"
  151.      is to be treated as ambiguous and will  be  flagged  as  a  syntax
  152.      error.   In  this  case  we  say  that   the   operator   (-)   is
  153.      non-associative.
  154.  
  155.      The standard approach in Gofer is to treat (-) as grouping to  the
  156.      left so that "1 - 2 - 3" will actually be treated as "(1-2)-3".
  157.  
  158. By  default,  every  operator   symbol   in   Gofer   is   treated   as
  159. non-associative with precedence 9.  These values can be  changed  by  a
  160. declaration of one of the following forms:
  161.  
  162.     infixl digit ops      to declare operators which group to the left
  163.     infixr digit ops      to declare operators which group to the right
  164.     infix  digit ops      to declare non-associative operators
  165.  
  166. In each of these declarations ops represents a  list  of  one  or  more
  167. operator symbols separated by commas and digit is an integer between  0
  168. and 9 which gives the precedence value for each of the listed  operator
  169. symbols.  The precedence digit may be omitted in which case a value  of
  170. 9 is assumed.  There are a number of restrictions on the use  of  these
  171. declarations:
  172.  
  173.   o  Operator  declarations  can  only  appear  in  files  of  function
  174.      definitions which are loaded into Gofer; they  cannot  be  entered
  175.      directly whilst using the Gofer interpreter.
  176.  
  177.   o  At most one operator declaration is permitted for  any  particular
  178.      operator symbol (even if repeated  declarations  all  specify  the
  179.      same precedence and grouping as the original declaration).
  180.  
  181.   o  Any file containing a declaration for an operator  precedence  and
  182.      grouping must also contain  a  (top-level)  declaration  for  that
  183.      operator.
  184.  
  185. In theory, it is possible to use an operator declaration at  any  point
  186. in a file of definitions.  In practice, it is sensible to  ensure  that
  187. each operator is declared before  the  symbol  is  used.   One  way  to
  188. guarantee this is to place all operator declarations at  the  beginning
  189. of the file [this condition is enforced in Haskell].  Note  that  until
  190. an operator declaration for a particular  symbol  is  encountered,  any
  191. occurrence of that symbol will be treated as a non-associative operator
  192. with precedence 9.
  193.  
  194.  
  195.  
  196.                                       10
  197.  
  198.  
  199.  
  200.  
  201. Introduction to Gofer         6. FUNCTION NAMES - IDENTIFIERS AND OPERATORS     
  202.  
  203.  
  204. The following operator declarations are taken from the standard prelude:
  205.  
  206.     -- Operator precedence table
  207.  
  208.     infixl 9 !!
  209.     infixr 9 .
  210.     infixr 8 ^
  211.     infixl 7 *
  212.     infix  7 /, `div`, `rem`, `mod`
  213.     infixl 6 +, -
  214.     infix  5 \\
  215.     infixr 5 ++, :
  216.     infix  4 ==, /=, <, <=, >=, >
  217.     infix  4 `elem`, `notElem`
  218.     infixr 3 &&
  219.     infixr 2 ||
  220.  
  221. and their use is illustrated by the following examples:
  222.  
  223.  Expression:     Equivalent to:   Reasons:
  224.  -----------     --------------   --------
  225.  1 + 2 - 3       (1 + 2) - 3      (+) and (-) have the same  precedence
  226.                                   and group to the left.
  227.  x : ys ++ zs    x : (ys ++ zs)   (:) and (++) have the same precedence
  228.                                   and group to the right
  229.  x == y || z     (x == y) || z    (==) has higher precedence than (||).
  230.  3 * 4 + 5       (3 * 4) + 5      (*) has higher precedence than (+).
  231.  y `elem` z:zs   y `elem` (z:zs)  (:) has higher precedence than elem.
  232.  12 / 6 / 3      syntax error     ambiguous  use  of  (/);  could  mean
  233.                                   either (12/6)/3 or 12/(6/3).
  234.  
  235. Note that function application always binds more tightly than any infix
  236. operator symbol.  For example, the expression "f x + g y" is equivalent
  237. to "(f x) + (g y)".  Another example which often causes problems is the
  238. expression  "f x + 1",  which is treated as "(f x)  +  1"  and  not  as
  239. "f (x+1)" as is sometimes expected.
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.                                       11
  263.  
  264.  
  265.